package cn.edu.sgu.www.mhxysy.controller;

import cn.edu.sgu.www.mhxysy.consts.MimeType;
import cn.edu.sgu.www.mhxysy.entity.School;
import cn.edu.sgu.www.mhxysy.pager.SchoolPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.SchoolService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/school", produces = MimeType.APPLICATION_JSON_CHARSET_UTF_8)
public class SchoolController {

	private final SchoolService schoolService;

	@Autowired
	public SchoolController(SchoolService schoolService) {
		this.schoolService = schoolService;
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(School school) {
		schoolService.insert(school);

		return JsonResult.success("添加成功");
	}

	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(School school) {
		schoolService.updateById(school);

		return JsonResult.success("修改成功");
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<School>> selectAll() {
		List<School> list = schoolService.selectAll();

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<School> selectById(@RequestParam Integer id) {
		School school = schoolService.selectById(id);

		return JsonResult.success(null, school);
	}

	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<School>> selectByPage(SchoolPager pager) {
		Page<School> page = schoolService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public JsonResult<String> upload(MultipartFile file) throws IOException {
		String url = schoolService.upload(file);

		return JsonResult.success(null, url);
	}

}